package com.test.ssm.article.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.ssm.article.entity.pojo.Article;
import com.test.ssm.article.entity.pojo.ArticleType;
import com.test.ssm.article.entity.vo.ResponseMessage;
import com.test.ssm.article.service.ArticleService;
import com.test.ssm.article.service.ArticleTypeService;
import com.test.ssm.article.util.helper.MyPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/article_type")
public class  SystemArticleType{
    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleTypeService articleTypeService;

    @GetMapping("/")
    public String getAll(@RequestParam(value = "newsState",required = false,defaultValue = "2") Byte newsState,
                         @RequestParam(value = "newsTitle",required = false,defaultValue = "") String newsTitle,
                         @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                         @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize,
                         Map<String,Object> map){
        //非动态sql按以下步骤执行
//        PageHelper.startPage(pageNum,pageSize);
//        MyPage<Article> page = articleService.getAll(newsState,newsTitle,pageNum,pageSize);
//        PageInfo<Article> pageInfo = new PageInfo<>(list);
//        map.put("pageInfo",pageInfo);
//        System.out.println("pageInfo = " + pageInfo.getTotal());
//        map.put("list",list);
        MyPage<Article> page = articleService.getAll(newsState,newsTitle,pageNum,pageSize);
        map.put("page",page);
        map.put("state",newsState);
        map.put("words",newsTitle);

        System.out.println("map = " + map);

        return "article/systemAticleType";
    }

    @GetMapping("/to_typeAdd")
    public String toAdd(Map<String,Object> map){
        List<ArticleType> types = articleTypeService.getAll();
        map.put("types",types);
        return "article/typeAdd";
    }

    @GetMapping("/check_is_exist")
    @ResponseBody
    public boolean checkIsExist(String title){
        return articleService.checkIsExist(title) != null;
    }

    @PostMapping("/typeAdd")
    @ResponseBody
    public ResponseMessage add(@Valid Article article, BindingResult bindingResult,
                               @RequestParam(value = "file",required = false)MultipartFile multipartFile, HttpServletRequest request){
        if(bindingResult.hasFieldErrors()){
            List<FieldError> errors = bindingResult.getFieldErrors();
            for (FieldError error:errors){
                System.out.println("error.getField() = " + error.getField());
                System.out.println("error.getDefaultMessage() = " + error.getDefaultMessage());
            }
            return new ResponseMessage().setSuccess(false).setCode(100).setMessage("验证失败").add("errors",errors);
        }
        return articleService.add(article,multipartFile,request);
    }
    @GetMapping("/to_typeUpdate")
    public String toUpdate(String newsId,Map<String,Object> map){
        Article article = articleService.getOneById(newsId);
        List<ArticleType> types = articleTypeService.getAll();
        map.put("types",types);
        map.put("article",article);
        return "article/typeUpdate";
    }

    @GetMapping("/delete")
    @ResponseBody
    public Boolean delete(String newsId){
        System.out.println("newsId1 = " + newsId);
        return articleService.deleteOneById(newsId) != null;
    }

    @GetMapping("/batch_delete")
    @ResponseBody
    public long batchDelete(String newsIds){
        System.out.println("newsId1 = " + newsIds);
        return articleService.batchDelete(newsIds);
    }
}



